<?php
include './php-ofc-library/open-flash-chart.php';

$link=mysql_connect ("localhost","sun","") or die ("Can not connect to db!".mysql_error());
mysql_select_db("jcat",$link) or die ("Can not select db!".mysql_error());


$Params = $_GET['Params'];
list($chart, $trader, $traderNo, $specialist, $level1, $level2, $game, $items, $start, $status) = split('[,]', $Params);

$traderRole = substr($trader, 0,3);

if($traderRole=='buy'){
	$shout = 'BidId';
}else if($traderRole=='sel'){
	$shout = 'AskId';
}

$Experiment = $level1.'_'.$level2.'_'.$game;

$items =(int)$items;
$step = 1;
if($items>20) $step = floor($items/20);

$accumulating = (int)$start+1;
$final = (int)$start + (int)$items -1;
$xLables = array();
while ( $accumulating <= ($items+(int)$start+1) )
{
	$xLables[] = $accumulating;
	$accumulating=$accumulating+$step;
}

if($traderNo > 0 & $traderNo < 50){
	$trader=$trader.'_'.$traderNo;	
}else{
	$trader=$trader.'%';
}

	$sql="SELECT SpecialistId FROM x_shout 
		WHERE TraderId LIKE '".$trader."' AND ExperimentId = '".$Experiment."' 
		ORDER BY Id ASC LIMIT ".$start.",".$items.";";

	$result = mysql_query($sql,$link) or die ("Can not read db!".mysql_error());
	$SpecialistId = array();

	$i=0;
	$j=0;
	$k=$start;
	$SpecialistTempName = '';
	$hbar = new hbar( '#43ACF7', '#284BFF' );
	
	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{	
		if($row['SpecialistId'] != $SpecialistTempName){
			$SpecialistId[$i]   = $row['SpecialistId'];
			$SpecialistTempName = $row['SpecialistId'];
			if($i == 1){
				$hbar->append_value( new hbar_value($start,$k) );			
			}elseif($i > 1){
				$hbar->append_value( new hbar_value($k-$j-1,$k) );
			}
			$j=0;
			$i++;
		}else{		
			$j++;
		}


		$k++;	
	}
	$hbar->append_value( new hbar_value($k-$j-1,$k) );
	
$SpecialistId = array_reverse($SpecialistId);


$title = new title( $Experiment.'  -  Traders Migration Feature - '.$trader.' - '.$specialist.'  ('.$start.' - '.$final.')' );

$chart = new open_flash_chart();
$chart->set_title( $title );

//
// we add the bars last so they are foremost
// (Z order)
//
$chart->add_element( $hbar );

//
$chart->add_y_axis( new y_axis() );

$x_legend = new x_legend( 'Shout times' );
$x_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_x_legend( $x_legend );

$y_legend = new y_legend( 'Specialists' );
$y_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_y_legend( $y_legend );

$x = new x_axis();
$x->set_colour( '#333333' );
//$x->set_grid_colour( '#D7E4A3' );
$x->set_offset( false );
$x->set_steps( $step );
$x->set_range( $start, $final+1 );
if($start >= 1000){
	$x_labels = new x_axis_labels();
	$x_labels->set_vertical();
	$x->set_labels( $x_labels );
}

$chart->set_x_axis( $x );

$y = new y_axis();
$y->set_offset( true );
$y->set_labels( $SpecialistId );

$chart->add_y_axis( $y );

echo $chart->toPrettyString();

?>
